//
//  SingerViewController.m
//  Coco_Music
//
//  Created by K。 on 15/6/22.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "SingerViewController.h"

@interface SingerViewController ()

@end

@implementation SingerViewController
-(void)dealloc
{
    [_collectionView release];
    [_SingerArray release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.SingerArray =[NSMutableArray array];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 20 - 40) / 3, (([UIScreen mainScreen].bounds.size.width - 20 - 40) / 3) );
    
    flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    
    flowLayout.minimumInteritemSpacing =  20;
    
    flowLayout.minimumLineSpacing = 20;
    

    flowLayout.headerReferenceSize =CGSizeMake(375, 50);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - self.navigationController.navigationBar.bounds.size.height - self.navigationController.tabBarController.tabBar.bounds.size.height-64) collectionViewLayout:flowLayout];

    flowLayout.headerReferenceSize =CGSizeMake([UIScreen mainScreen].bounds.size.width , 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - self.navigationController.navigationBar.bounds.size.height - self.navigationController.tabBarController.tabBar.bounds.size.height) collectionViewLayout:flowLayout];

    self.collectionView .backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.collectionView ];
    [ self.collectionView release];

    
    
    self.collectionView .delegate = self;
    self.collectionView .dataSource = self;
    [ self.collectionView  registerClass:[SingerCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self getData];
    
    self.HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    

}

-(void)getData

{
        NSString *url =@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=5df9002635a21cc5f7f660e71a16d1e35e5a04a0&idfa=B5F4461B-F9C7-4F1E-BA92-EB2E683311DF&utdid=VYEt7JjHCIUDAGowp1W3cSW3&alf=201200&bundle_id=com.ttpod.music";
   
    [AFN_block NSString:url Blokc:^(id request) {
        NSMutableDictionary *myDIc =[NSMutableDictionary dictionaryWithDictionary:request];
      NSLog(@"歌手   === %@",myDIc);
        NSMutableArray *DataArray =[myDIc objectForKey:@"data"];
        for (NSMutableDictionary *myDic in DataArray) {
            Singer *singer =[[Singer alloc]init];
            singer.title =[myDic objectForKey:@"title"];
            singer.big_pic_url =[myDic objectForKey:@"big_pic_url"];
            singer.pic_url =[myDic objectForKey:@"pic_url"];
            singer.SingeID =[[myDic objectForKey:@"id"]integerValue];
            [self.SingerArray addObject:singer];
            [singer release];
            
        }
        [self.HUD hide:YES];
        [self.collectionView reloadData];
       
        
        
    }];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myTable deselectRowAtIndexPath:indexPath animated:YES];
    SingerDetailViewController *singDVC =[[SingerDetailViewController alloc]init];
    singDVC.mySinger =[self.SingerArray objectAtIndex:indexPath.row];
    singDVC.SingerDetailID =[[self.SingerArray objectAtIndex:indexPath.row]SingeID];
    [self.navigationController pushViewController:singDVC animated:YES];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

       return self.SingerArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{   SingerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse"  forIndexPath:indexPath];
  
  cell.singer =[self.SingerArray objectAtIndex:indexPath.row];
    
    
    return  cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
